﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Geometries
{
    /// <summary>
    /// 描述一系列点的集合
    /// </summary>
    public class Point2DCollection : IEnumerable<Point2D>
    {
        private Point2D[] points;
        public Point2DCollection()
        {
            points = new Point2D[0];
        }
        public Point2DCollection(IEnumerable<Point2D> p_points)
        {
            if (p_points == null)
            {
                throw new ArgumentNullException(nameof(p_points));
            }

            List<Point2D> list = new List<Point2D>(p_points);
            points = list.ToArray();
        }
        /// <summary>
        /// 点数量
        /// </summary>
        public int Count
        {
            get
            {
                if (points != null)
                {
                    return points.Length;
                }
                return 0;
            }
        }

        public Point2D this[int index]
        {
            get { return points[index]; }
            set { points[index] = value; }
        }

        public Point2D[] ToArray()
        {
            return points;
        }
        /// <summary>
        /// 将其他点附加到当前集合末尾
        /// </summary>
        /// <param name="p_points"></param>
        public void Append(IEnumerable<Point2D> p_points)
        {
            List<Point2D> list = new List<Point2D>(p_points);
            var ps = new Point2D[points.Length + list.Count];
            var nowsize = points.Length;
            for (int i = 0; i < nowsize; i++)
            {
                ps[i] = points[i];
            }
            for (int i = 0; i < list.Count; i++)
            {
                ps[i + nowsize] = list[i];
            }
            points = ps;
        }


        public IEnumerator<Point2D> GetEnumerator()
        {
            foreach (var item in points)
            {
                yield return item;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            foreach (var item in points)
            {
                yield return item;
            }
        }
    }
}
